using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HappyFrogXna;
using HappyFrogXna.Roles;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using HappyFrogXna.Scene;
using HappyFrogXna.ScreenSystem;

namespace HappyFrogXna.Level
{
    class GameLevel1 : ILevel
    {
        private World _world;
        private IScene _scene;
        public IScene Scene { get { return _scene; } }
        private List<Vector2> _frogStratPos;

        public GameLevel1(World w)
        {
            _world = w;
            _scene = SceneCreator.CreateScene("BasicScene", w);
            _frogStratPos = new List<Vector2>();
        }

        public void ResetFrogPositong(List<Entity> frogs)
        {
            // frogs[0] is always the one on the slingshot
            //frogs[0].Position = new Vector2(_scene.SlingshotCenterPos.X, _scene.SlingshotCenterPos.Y);
            //frogs[0].Rotation = 0.0f;
            for (int i = 0; i < frogs.Count; ++i)
            {
                frogs[i].Position = _frogStratPos[i];
                frogs[i].Rotation = 0.0f;
            }
        }

        private void recodeFrogStartPos(List<Entity> frogs)
        {
            _frogStratPos.Clear();
            for (int i = 0; i < frogs.Count; ++i)
            { 
                _frogStratPos.Add(frogs[i].OriginalPos);
            }            
        }

        public void EnterLevel(out EntityAssemble entities)
        {
            List<Entity> frogs = new List<Entity>();
            List<Entity> objects = new List<Entity>();
            List<Entity> animals = new List<Entity>();

            frogs.Add(new Frog1(_world, new Vector2(_scene.SlingshotCenterPos.X, _scene.SlingshotCenterPos.Y)));
            frogs[0].Rotation = 0.0f;
            frogs[0].IsActive = false;
            frogs[0].IsVisible = true;
            frogs.Add(new Frog1(_world, new Vector2(12.76486f, 22.75425f)));
            frogs[1].Rotation = 0.0f;
            frogs[1].IsActive = false;
            frogs[1].IsVisible = true;
            frogs.Add(new Frog1(_world, new Vector2(10.64998f, 22.69634f)));
            frogs[2].Rotation = 0.0f;
            frogs[2].IsActive = false;
            frogs[2].IsVisible = true;
            frogs.Add(new Frog1(_world, new Vector2(8.208312f, 22.69637f)));
            frogs[3].Rotation = 0.0f;
            frogs[3].IsActive = false;
            frogs[3].IsVisible = true;
            recodeFrogStartPos(frogs);

            objects.Add(new RectStone2(_world, new Vector2(104.1592f, 22.625f)));
            objects[0].Rotation = 0f;
            objects[0].IsActive = true;
            objects[0].IsVisible = true;
            objects.Add(new RectStone2(_world, new Vector2(58.75267f, 22.625f)));
            objects[1].Rotation = 0f;
            objects[1].IsActive = true;
            objects[1].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(94.97298f, 22.69135f)));
            objects[2].Rotation = 6.332482E-05f;
            objects[2].IsActive = true;
            objects[2].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(99.91132f, 22.69119f)));
            objects[3].Rotation = -0.0001331728f;
            objects[3].IsActive = true;
            objects[3].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(89.9111f, 22.69124f)));
            objects[4].Rotation = 7.218385E-06f;
            objects[4].IsActive = true;
            objects[4].IsVisible = true;
            objects.Add(new RectIce3(_world, new Vector2(92.40403f, 20.05084f)));
            objects[5].Rotation = 0.0002875591f;
            objects[5].IsActive = true;
            objects[5].IsVisible = true;
            objects.Add(new RectIce3(_world, new Vector2(97.41104f, 20.05145f)));
            objects[6].Rotation = -6.678629E-05f;
            objects[6].IsActive = true;
            objects[6].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(92.78342f, 17.62958f)));
            objects[7].Rotation = 0.0006094858f;
            objects[7].IsActive = true;
            objects[7].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(97.18285f, 17.63004f)));
            objects[8].Rotation = -0.0009303422f;
            objects[8].IsActive = true;
            objects[8].IsVisible = true;
            objects.Add(new RectWood3(_world, new Vector2(94.97108f, 15.20859f)));
            objects[9].Rotation = 0.0001260175f;
            objects[9].IsActive = true;
            objects[9].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(94.92091f, 13.31848f)));
            objects[10].Rotation = 0.000441287f;
            objects[10].IsActive = true;
            objects[10].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(90.95705f, 17.4074f)));
            objects[11].Rotation = 0.0007174212f;
            objects[11].IsActive = true;
            objects[11].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(90.96127f, 13.20461f)));
            objects[12].Rotation = 0.000370032f;
            objects[12].IsActive = true;
            objects[12].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(99.09676f, 17.40888f)));
            objects[13].Rotation = -0.002366077f;
            objects[13].IsActive = true;
            objects[13].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(99.08157f, 13.20623f)));
            objects[14].Rotation = -0.002324938f;
            objects[14].IsActive = true;
            objects[14].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(61.97356f, 23.44139f)));
            objects[15].Rotation = 0.0003123123f;
            objects[15].IsActive = true;
            objects[15].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(62.03545f, 20.73901f)));
            objects[16].Rotation = 0.001099845f;
            objects[16].IsActive = true;
            objects[16].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(62.08366f, 18.03444f)));
            objects[17].Rotation = 1.574025f;
            objects[17].IsActive = true;
            objects[17].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(64.53593f, 22.90996f)));
            objects[18].Rotation = 5.599032E-05f;
            objects[18].IsActive = true;
            objects[18].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(68.78598f, 22.91f)));
            objects[19].Rotation = 1.147965E-05f;
            objects[19].IsActive = true;
            objects[19].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(73.41206f, 22.91002f)));
            objects[20].Rotation = -0.0001191375f;
            objects[20].IsActive = true;
            objects[20].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(77.97928f, 22.90998f)));
            objects[21].Rotation = -3.692248E-05f;
            objects[21].IsActive = true;
            objects[21].IsVisible = true;
            objects.Add(new RectWood3(_world, new Vector2(66.26305f, 20.48872f)));
            objects[22].Rotation = 1.588251E-05f;
            objects[22].IsActive = true;
            objects[22].IsVisible = true;
            objects.Add(new RectWood3(_world, new Vector2(75.82688f, 20.48873f)));
            objects[23].Rotation = 1.14259E-05f;
            objects[23].IsActive = true;
            objects[23].IsVisible = true;
            objects.Add(new RectWood1(_world, new Vector2(71.03082f, 20.33238f)));
            objects[24].Rotation = 0.0001610716f;
            objects[24].IsActive = true;
            objects[24].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(66.77636f, 18.06749f)));
            objects[25].Rotation = 1.473688E-05f;
            objects[25].IsActive = true;
            objects[25].IsVisible = true;
            objects.Add(new RectWood2(_world, new Vector2(77.30295f, 18.06752f)));
            objects[26].Rotation = 2.472127E-05f;
            objects[26].IsActive = true;
            objects[26].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(71.28878f, 18.285f)));
            objects[27].Rotation = 0.001207355f;
            objects[27].IsActive = true;
            objects[27].IsVisible = true;
            objects.Add(new RectWood1(_world, new Vector2(68.27837f, 15.23963f)));
            objects[28].Rotation = -0.1099608f;
            objects[28].IsActive = true;
            objects[28].IsVisible = true;
            objects.Add(new RectWood1(_world, new Vector2(75.37238f, 15.28377f)));
            objects[29].Rotation = 0.08958486f;
            objects[29].IsActive = true;
            objects[29].IsVisible = true;
            objects.Add(new RectStone4(_world, new Vector2(83.41137f, 22.1875f)));
            objects[30].Rotation = 0f;
            objects[30].IsActive = true;
            objects[30].IsVisible = true;
            objects.Add(new RectStone4(_world, new Vector2(76.72387f, 1.8125f)));
            objects[31].Rotation = 0f;
            objects[31].IsActive = true;
            objects[31].IsVisible = true;
            objects.Add(new RectWood4(_world, new Vector2(87.35284f, 22.34674f)));
            objects[32].Rotation = 0.001439407f;
            objects[32].IsActive = true;
            objects[32].IsVisible = true;
            objects.Add(new RectWood1(_world, new Vector2(85.39534f, 19.10822f)));
            objects[33].Rotation = 0.04216249f;
            objects[33].IsActive = true;
            objects[33].IsVisible = true;
            objects.Add(new RectIce5(_world, new Vector2(80.97598f, 23.43843f)));
            objects[34].Rotation = 0.0008371739f;
            objects[34].IsActive = true;
            objects[34].IsVisible = true;
            objects.Add(new RectIce5(_world, new Vector2(80.97471f, 18.02953f)));
            objects[35].Rotation = 0.003745773f;
            objects[35].IsActive = true;
            objects[35].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(81.04698f, 20.73431f)));
            objects[36].Rotation = 0.002153769f;
            objects[36].IsActive = true;
            objects[36].IsVisible = true;
            objects.Add(new RectWood5(_world, new Vector2(81.04353f, 15.32553f)));
            objects[37].Rotation = 0.005117552f;
            objects[37].IsActive = true;
            objects[37].IsVisible = true;
            objects.Add(new RectStone1(_world, new Vector2(71.45676f, 10.75f)));
            objects[38].Rotation = 0f;
            objects[38].IsActive = true;
            objects[38].IsVisible = true;
            objects.Add(new RectStone1(_world, new Vector2(75.70676f, 10.75f)));
            objects[39].Rotation = 0f;
            objects[39].IsActive = true;
            objects[39].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(72.1573f, 7.952369f)));
            objects[40].Rotation = 0.001686727f;
            objects[40].IsActive = true;
            objects[40].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(76.72588f, 7.951361f)));
            objects[41].Rotation = -0.002883141f;
            objects[41].IsActive = true;
            objects[41].IsVisible = true;
            objects.Add(new RectIce3(_world, new Vector2(74.41399f, 5.311593f)));
            objects[42].Rotation = -0.0002217664f;
            objects[42].IsActive = true;
            objects[42].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(70.41137f, 7.951635f)));
            objects[43].Rotation = -0.002528532f;
            objects[43].IsActive = true;
            objects[43].IsVisible = true;
            objects.Add(new RectWood3(_world, new Vector2(71.56104f, 16.39393f)));
            objects[44].Rotation = 0.002637709f;
            objects[44].IsActive = true;
            objects[44].IsVisible = true;
            objects.Add(new RectIce2(_world, new Vector2(71.83207f, 13.75036f)));
            objects[45].Rotation = 0.002606877f;
            objects[45].IsActive = true;
            objects[45].IsVisible = true;

            animals.Add(new Animal2(_world, new Vector2(95.06555f, 10.94936f)));
            animals[0].Rotation = 0.3932845f;
            animals[0].IsActive = true;
            animals[0].IsVisible = true;
            animals.Add(new Animal1(_world, new Vector2(85.34887f, 23.69125f)));
            animals[1].Rotation = 0f;
            animals[1].IsActive = true;
            animals[1].IsVisible = true;
            animals.Add(new Animal2(_world, new Vector2(74.41137f, 8.953774f)));
            animals[2].Rotation = 0f;
            animals[2].IsActive = true;
            animals[2].IsVisible = true;
            animals.Add(new Animal2(_world, new Vector2(97.3418f, 23.69128f)));
            animals[3].Rotation = 0f;
            animals[3].IsActive = true;
            animals[3].IsVisible = true;
            animals.Add(new Animal1(_world, new Vector2(95.44008f, 18.49356f)));
            animals[4].Rotation = 0.3894941f;
            animals[4].IsActive = true;
            animals[4].IsVisible = true;
            animals.Add(new Animal1(_world, new Vector2(66.5918f, 23.68791f)));
            animals[5].Rotation = 0f;
            animals[5].IsActive = true;
            animals[5].IsVisible = true;
            animals.Add(new Animal2(_world, new Vector2(75.0758f, 18.9317f)));
            animals[6].Rotation = 0.3922455f;
            animals[6].IsActive = true;
            animals[6].IsVisible = true;

            entities = new EntityAssemble(frogs, objects, animals);
        }

        public void LeaveLevel(EntityAssemble entities)
        {
            entities.Frogs.Clear();
            entities.Objects.Clear();
            entities.Animals.Clear();
        }
    }
}
